package aidansmeaton.airport;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class NewPlaneActivity extends CursorListActivity {
		
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_plane);
		
		cursor = db.getPlanes();
		Log.w("PLANES", "" + cursor.getCount());

		setListAdapter(new NewPlaneAdapter(this, cursor));
		
		ListView list = this.getListView();
		
		list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	/*
            	TextView stopnameTextView = (TextView) view.findViewById(R.id.favourites_stopname);
            	CharSequence stopName = stopnameTextView.getText();
            	
            	BusStop stop = new BusStop((String) view.getTag(R.id.stopcode), stopName.toString(), true);
            	
            	qa = new BusStopListQuickAction(view, stop, FavouritesActivity.this);
            	qa.show();
            	*/
            }
		});
	}
	
	/**
	 * Adapter class for a database cursor to the
	 * users favourite bus stops.
	 * @author Euan Freeman
	 */
	private class NewPlaneAdapter extends CursorAdapter {
		
		private LayoutInflater inflater;
		
		public NewPlaneAdapter(Context context, Cursor cursor) {
			super(context, cursor);
			
			inflater = LayoutInflater.from(context);
						
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			
			//view.setTag(R.id.stopcode, cursor.getString(DatabaseHelper.BUS_STOP_CODE));
			
			TextView planeName = (TextView) view.findViewById(R.id.newplane_name);
			TextView planeRange = (TextView) view.findViewById(R.id.newplane_range);
			TextView planeSeats = (TextView) view.findViewById(R.id.newplane_seats);		
			TextView planePrice = (TextView) view.findViewById(R.id.newplane_price);		

			planeName.setText(cursor.getString(2));
			planeRange.setText(cursor.getInt(4) + "nm");
			planeSeats.setText(cursor.getInt(3) + "");
			planePrice.setText("$" + cursor.getInt(0));
						
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			return inflater.inflate(R.layout.new_plane_row, null);
		}
	}

}
